<?php include_once('header.php');?>
<div class="main-content">
		<div class="breadcrumbs" id="breadcrumbs">
			<script type="text/javascript">
				try{ace.settings.check('breadcrumbs' , 'fixed')}catch(e){}
			</script>
			<ul class="breadcrumb">
				<li>
					<i class="icon-home home-icon"></i>
					会员管理
				</li>
				<li>
					报股权
				</li>
			</ul>
		</div>	
		<div class="page-content">
             <div class="visible-md visible-lg hidden-sm hidden-xs btn-group">
				<div style="float:left;margin-right:8px;">
					<?php if($authority['add_status'] == 2) {?>
					<a onclick="selectAll()"><span class="btn btn-xs btn-inverse">全选/取消</span></a>
					<a onclick="deleteAll()"><span class="btn btn-xs btn-purple">删除选中</span></a>	
					<?php }?>
						
				</div>
				<div class="page-header" style="height:40px;"></div>
			</div>
			<div class="page-header">
				<h1 class="blue"><i class="icon-edit"></i> 报股权</h1>
			</div>
			<div class="visible-md visible-lg hidden-sm hidden-xs btn-group" style="margin-bottom:20px;">
				
			</div>
			<div class="row">
				<div class="col-xs-12">
					<div class="table-header">
						报股权
					</div>
					<div class="table-responsive">
					<form id="bonus_form" action="/admin/user/doGuquan" method="post">
					<input type="hidden" name="chuzhang_name" id="chuzhang_name" >
					<input type="hidden" name="checids" id="checids" >
					<input type="hidden" name="user_id" id="user_id" value="<?php echo $id;?>"/>
					<input type="hidden" name="frbs" id="frbs" >
						<table id="sample-table-2" class="table table-striped table-bordered table-hover">
							<thead>
								<tr>
									<th>选择</th>   
									<th>id</th>                      
									<th>帐户</th>
									<th>姓名</th>
									<th>层级</th>
									<th>级别</th>
									<th>下级个数</th>
									<th>供位编号</th>
									<th>费用</th>
									<th>报单时间</th>
									<th>业绩</th>                                    
									<th>总佣金</th>
									<th>报单币</th> 
									<th>报单币余额</th>
									<th >分润比例%</th>   
									<th>分润金额</th>  
									<th>备注</th>                        
									<th>操作</th>
								</tr>
							</thead>
							<tfoot>
								<tr>
									<th>选择</th>   
									<th>id</th>                      
									<th>帐户</th>
									<th>姓名</th>
									<th>层级</th>
									<th>级别</th>
									<th>下级个数</th>
									<th>供位编号</th>
									<th>费用</th>
									<th>报单时间</th>
									<th>业绩</th>                                    
									<th>总佣金</th>
									<th>报单币</th>
									<th>报单币余额</th>  
									<th >分润比例%</th>  
									<th>分润金额</th>  
									<th>备注</th>                    
									<th>操作</th>
								</tr>					
							</tfoot>


						</table>
						</form>
					</div><!-- /.table-responsive -->
				</div><!-- /span -->
				
			</div>
			<div class="visible-md visible-lg hidden-sm hidden-xs btn-group" style="">
			<div style="float:left;margin-right:8px;width: 100%; margin-bottom: 8px;display:none;">
			等级分润比设置:
			<?php foreach ($guquan_level as $key=>$value){
			echo $value['name'].':';
			    ?>
			<input type="number" name="guquan_level"  guquan_id="<?php echo $value['id'];?>" cengji="<?php echo $value['cengji'];?>" user_level="<?php echo $value['level_id'];?>" value="<?php echo $value['bili'];?>"/>
			<?php }?>
			</div>
			<br>
			<div style="float:left;margin-right:8px;">
				<?php if($authority['add_status'] == 2) {?>
				<a onclick="selectAll()"><span class="btn btn-xs btn-inverse">全选/取消</span></a>	
				<?php }?>
			</div>
			&nbsp;分润总额：<input type="text" id="bonus_money" onchange="fixed3(this)"/>&nbsp;<a onclick="piliang_money()"><span class="btn btn-xs btn-pink">计算分润</span></a>&nbsp;备注：<input type="text" id="remark"/>&nbsp;<a onclick="piliang_remark()">&nbsp;<span class="btn btn-xs btn-purple">批量备注</span></a>&nbsp;出账人：<input type="text" id="chuzhang" value=""/>	<a onclick="bonus_money()">&nbsp;<span class="btn btn-xs btn-purple">确认分润</span></a>
		</div>
		</div>
										
	</div>
<?php include_once('content.php');?>
<link  rel="stylesheet" href="/public_source/www/assets/css/jquery-confirm.css" />
	<link  rel="stylesheet" href="/public_source/www/css/confirm.css" />
	<script src="/public_source/www/assets/js/jquery.dataTables.min.js"></script>
	<script src="/public_source/www/assets/js/jquery.dataTables.bootstrap.js"></script>
        <script src="/public_source/www/assets/js/jquery-confirm.js"></script>
        <script src="/public_source/www/My97DatePicker/WdatePicker.js"></script>
        <script src="/public_source/www/js/charge.js"></script>
<?php include_once('footer.php');?>	
<script type="text/javascript">
    var message = "<?php echo $message;?>";
    if(message == 2) {var con = '修改成功,请确定';}else if(message == 3){var con = '修改失败,请重试';}else if(message ==4){var con = '添加成功,请确认';}else if(message ==5) {var con = '添加失败,请重试';}else if(message==6){ var con='参数错误，请重试';};
    if(message != 1 && message.length>0) {
            $.alert({
                title: '提示',
                content: con,
                confirm: function(){
                    window.location.replace('/admin/user/user_list');
                }
            });
    };
    jQuery(function($) {
        
        var parent_id =$("#parent_id").val();
        var id =$("#user_id").val();
		var start_time = $("#start_time").val();
        
		
        var oTable1 = $('#sample-table-2').dataTable({ "oLanguage": {"sUrl": "/public_source/www/assets/language/zh_CN.json"},			     	

		"aaSorting": [[ 1, "asc" ]],
		"bAutoWidth": false, 
		"bProcessing": true, //开启读取服务器数据时显示正在加载中……特别是大数据量的时候，开启此功能比较好
		"bServerSide": true, //开启服务器模式，使用服务器端处理配置datatable。注意：sAjaxSource参数也必须被给予为了给datatable源代码来获取所需的数据对于每个画。 这个翻译有点别扭。开启此模式后，你对datatables的每个操作 每页显示多少条记录、下一页、上一页、排序（表头）、搜索，这些都会传给服务器相应的值。 
		"sAjaxSource": "/admin/user/ajax_guquan_list?id="+id, //给服务器发请求的url
		"aoColumns": [ //这个属性下的设置会应用到所有列，按顺序没有是空
       {"mData": 'check'},
        {"mData": 'id'}, //mData 表示发请求时候本列的列明，返回的数据中相同下标名字的数据会填充到这一列
        {"mData": 'username'},
        {"mData": 'true_name'},
        {"mData":'cengji_name'},
		{"mData": 'user_level_name'},
		{"mData": 'next_count'},
		{"mData": 'csg'},
		{"mData": 'price'},
        {"mData": 's_time'},
		{"mData": 'performance'},
	 	{"mData": 'money'},
        {"mData": 'system_money'},
        {"mData": 'system_balance'},
        {"mData": 'fenru_bili'},
        {"mData": 'bonus_money'},
        {"mData": 'remark'},
        {"mData": 'operate'},
		
		],
		"aoColumnDefs": [//和aoColums类似，但他可以给指定列附近爱属性
		{"bSortable": false, "aTargets": [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]}, //这句话意思是第1,3,6,7,8,9列（从0开始算） 不能排序
     ]});							
		$('[data-rel="tooltip"]').tooltip({placement: tooltip_placement});
		function tooltip_placement(context, source) {
			var $source = $(source);
			var $parent = $source.closest('table')
			var off1 = $parent.offset();
			var w1 = $parent.width();	
			var off2 = $source.offset();
			var w2 = $source.width();
			if( parseInt(off2.left) < parseInt(off1.left) + parseInt(w1 / 2)) return 'right';
			return 'left';
		}
	});

    function selectAll() { //全选/取消
		var a = document.getElementsByName("c_id");
		if(a[0].checked){
			for(var i = 0;i<a.length;i++){
				if(a[i].type == "checkbox") a[i].checked = false;
			}
		}else{
			for(var i = 0;i<a.length;i++){
				if(a[i].type == "checkbox") a[i].checked = true;
			}
		}
	}

	function piliang_money(){
		var bonus_money=$("#bonus_money").val();
		if(bonus_money){
			var b=document.getElementsByName('guquan_level');
			var fenrunbi=new Array();
			var frbs='';
			for(var j=0;j<b.length;j++){
				fenrunbi[$(b[j]).attr('user_level')+'_'+$(b[j]).attr('cengji')]=b[j].value;
				frbs+=$(b[j]).attr('user_level')+'_'+$(b[j]).attr('cengji')+'_'+b[j].value+':';
			}
			$("#frbs").val(frbs);
			
		var a = document.getElementsByName("c_id");
		var fenrun_money=0;
		for(var i=0;i<a.length;i++){
			if(a[i].checked ==true){
				if($("#fenru_bili_"+a[i].value).val()){
					//fenrun_money=bonus_money*fenrunbi[$(a[i]).attr('user_level')+'_'+$(a[i]).attr('cengji')];
					fenrun_money=bonus_money*(parseFloat($("#fenru_bili_"+a[i].value).val()/100));
					fenrun_money=fenrun_money.toFixed(4);
					$("#bonus_money_"+a[i].value).val(fenrun_money.substring(0,fenrun_money.toString().length - 1));
				}else{
					a[i].checked = false;
					$("#bonus_money_"+a[i].value).val('');
				}
			}else{
				$("#bonus_money_"+a[i].value).val('');
			}
		}
		}else{
			$.alert({
                title: '提示',
                content: '请填写分红金额',
                confirm: function(){
                    
                }
            });
		}
	}
	function piliang_remark(){
		var chuzhang=1;
		var bonus_money=$("#bonus_money").val();
		var remark_text=$("#remark").val();
		if(bonus_money){
			if(chuzhang){
				var a = document.getElementsByName("c_id");
				var start_time=$("#start_time").val();
				if(start_time){
					start_time=start_time.split("-");
					//console.log(start_time[1]);
					start_time=start_time[0]+'年'+start_time[1]+'月份';
				}else{
					var date=new Date;
					var mounth=parseFloat(date.getMonth()+1);
					start_time=date.getFullYear()+'年'+mounth+'月份';
					
				}
				var shifa=0;
				var yue='';
				var remark_text1='';
				for(var i=0;i<a.length;i++){
					if(a[i].checked ==true){
						if(!remark_text){
							shifa=parseFloat($("#bonus_money_"+a[i].value).val())*0.95;
							yue=shifa+parseFloat($("#remark_"+a[i].value).attr('system_money'));
							remark_text1='分红扣5%转币费用,实得分润'+shifa+'报单币,报单币余额为'+yue;
							$("#remark_"+a[i].value).val(remark_text1);
						}else{
							$("#remark_"+a[i].value).val(remark_text);
						}
						
					}else{
						$("#remark_"+a[i].value).val('');
					}
				}
				}else{
					$.alert({
		                title: '提示',
		                content: '请填写出账人',
		                confirm: function(){
		                    
		                }
		            });
				}
			}else{
				$.alert({
	                title: '提示',
	                content: '请填写分红金额',
	                confirm: function(){
	                    
	                }
	            });
			}
	}

	function bonus_money(){
		var chuzhang=$("#chuzhang").val();
		var bonus_money=$("#bonus_money").val();
		if(chuzhang){
			var a = document.getElementsByName("c_id");
			var total_bonus=0;
			var num=0;
			var checids=new Array();
			var j=0;
			for(var i=0;i<a.length;i++){
				if(a[i].checked ==true){
					checids[j]=a[i].value;
					num=parseInt(i)+parseInt(1);
					total_bonus=parseFloat(total_bonus)+parseFloat($("#bonus_money_"+a[i].value).val());
					
					j++;
				}
			}
			if(total_bonus >0){
				var data={'username':chuzhang,'bonus_money':total_bonus};
				$.ajax({
					url:"/admin/bonus/checkMoney",
					async: false,
					type:"post",
					data: data,
					dataType: 'json',
					success:function(data) {
						if(data.flag==false){
							$.alert({
				                title: '提示',
				                content: data.errorMsg,
				                confirm: function(){
				                    
				                }
				            });
						}else{
							$("#chuzhang_name").val(chuzhang);
							$("#checids").val(checids.join(','));
							$("#bonus_form").submit();
						}
					}
				});
			}else{
				$.alert({
	                title: '提示',
	                content: '分红金额必须大于0',
	                confirm: function(){
	                    
	                }
	            });
			}
		}else{
			$.alert({
                title: '提示',
                content: '请填写出账人',
                confirm: function(){
                    
                }
            });
		}	
	}
	function jisuan_fenrun(object){
		var bonus_money=$("#bonus_money").val();
		if(bonus_money){
			var bili=$(object).val();
			if(bili){
				bili=bili/100;
				var fenrunmoney=bonus_money*bili;
                fenrunmoney=fenrunmoney.toFixed(4);
				$("#bonus_money_"+$(object).attr('user_id')).val(fenrunmoney.substring(0,fenrunmoney.toString().length - 1));
			}
		}else{
			$.alert({
                title: '提示',
                content: '请填写分润总额',
                confirm: function(){
                    
                }
            });
		}
	}

	function fixed3(object) {
        var value=parseFloat($(object).val());

        value=value.toFixed(4);
        $(object).val(value.substring(0,value.toString().length - 1));
    }
</script>
<style>
	#sample-table-2_filter {
		display:none;
	}
</style>